<?php

namespace App\HttpController;


use App\Common\Sys;
use App\Common\SysContext;
use EasySwoole\Http\AbstractInterface\AbstractRouter;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use FastRoute\RouteCollector;
use XWX\Common\H;
use XWX\Common\XReturn;

class Router extends AbstractRouter
{
    public function initialize(RouteCollector $routeCollector)
    {
//        $this->setGlobalMode(true);

//        $this->setMethodNotAllowCallBack(function (Request $request, Response $response)
//        {
//            $response->write('未找到处理方法');
//            return false;//结束此次响应
//        });

        $this->setRouterNotFoundCallBack(function (Request $request, Response $response)
        {
            $r = new XReturn();
            $r->errcode = -404;
            $r->errmsg = 'Not Found';

            $response->write(H::funcToStr($r));
            return false;//重定向到index路由
        });


        //index
        $routeCollector->addRoute(['GET', 'POST'], '/', '/Index');
        //wx
        $routeCollector->addRoute(['GET', 'POST'], '/wx/center', '/Wx/Center');
        $routeCollector->addRoute(['GET', 'POST'], '/wx/oauth_re', '/Wx/Oauth/OauthRe');
        $routeCollector->addRoute(['GET', 'POST'], '/wx/oauth_callback', '/Wx/Oauth/OauthCallback');

        //oa
        $routeCollector->addRoute(['GET', 'POST'], '/oa/{page}[/{func}]', function (Request $request, Response $response)
        {
            $page = $request->getQueryParam('page');
            $func = $request->getQueryParam('func');

            $route = "/oa/{$page}";
            if (H::funcStrHasAnyText($func))
            {
                $route = "/oa/{$page}/{$func}";
            }

            return $route;
        });

        //后台应用
        $routeCollector->addRoute(['GET', 'POST'], '/{app_id}/admin/{page}[/{func}]', function (Request $request, Response $response)
        {
            $app_id = $request->getQueryParam('app_id');
            $page = $request->getQueryParam('page');
            $func = $request->getQueryParam('func');


            $path_fix = $this->funcGetAppPathFix($app_id);

            $route = $this->funcGetRoutePath(true, $path_fix, $app_id, $page, $func);


            return $route;
        });


        //web
        $routeCollector->addRoute(['GET', 'POST'], '/{app_id}/web_{key}/{page}[/{func}]', function (Request $request, Response $response)
        {
            $app_id = $request->getQueryParam('app_id');
            $page = $request->getQueryParam('page');
            $func = $request->getQueryParam('func');

            $path_fix = $this->funcGetAppPathFix($app_id);


            $route = $this->funcGetRoutePath(false, $path_fix, $app_id, $page, $func, false);
            if (H::funcStrToLower($page) == 'sys')
            {
                $route = "/Sys/{$func}";
            }

            //模板
            if (H::funcStrStartsWith($page, 'f__'))
            {
                $route = "/Sys/Web/F_file";
            }


            //获取URL
            $url = substr($request->getUri()->getPath(), 1, strlen($request->getUri()->getPath()));
            if (H::funcStrHasAnyText($request->getUri()->getQuery()))
            {
                $url = "{$url}?{$request->getUri()->getQuery()}";
            }
            SysContext::funcInsStatic()->set('url', $url);


            return $route;
        });


        //wxapp
        $routeCollector->addRoute(['GET', 'POST'], '/{app_id}/wxapp/{page}[/{func}]', function (Request $request, Response $response)
        {
            $app_id = $request->getQueryParam('app_id');
            $page = $request->getQueryParam('page');
            $func = $request->getQueryParam('func');


            $path_fix = 'XAppWxapp';

            $route = "/{$path_fix}/{$app_id}/web/{$page}";
            if (H::funcStrHasAnyText($func))
            {
                $route = "/{$path_fix}/{$app_id}/web/{$page}/{$func}";
            }


            return $route;
        });
    }


    function funcGetRoutePath($is_admin, $path_fix, $app_id, $page, $func = null)
    {
        $app_id = H::funcStrToCamelize($app_id);
        $page = H::funcStrToCamelize($page);

        $center = 'Admin';
        if ($is_admin == false)
        {
            $center = 'Web';
        }


        //如：/app_admin/admin/app_admin/edit
        //如：/Cashbook/web/api/
        $route = "/{$path_fix}/{$app_id}/{$center}/{$page}";
        if (H::funcStrHasAnyText($func))
        {
//            $func = H::funcStrToCamelize($func);
            $route = "/{$path_fix}/{$app_id}/{$center}/{$page}/{$func}";
        }

        return $route;
    }


    function funcGetAppPathFix($app_id)
    {
        $path_fix = 'XApp';
        if (Sys::funcIsSysApp($app_id))
        {
            $path_fix = 'XAppSystem';
        }

        if (Sys::funcIsWxApp($app_id))
        {
            $path_fix = 'XAppWxapp';
        }


        return $path_fix;
    }
}